package com.dankegongyu.lib.paging

import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.dankegongyu.customer.common.base.paging.test.TestPagingVM
import com.dankegongyu.lib.base.BaseAppActivity
import com.dankegongyu.lib.common.paging.PagingData
import com.dankegongyu.lib.common.paging.PagingState

/**
 * @author wupuquan
 * @version 1.0
 * @since 2020/4/18 15:20
 */
class TestPaging2 : BaseAppActivity() {

    private var mPagingViewModel: TestPagingVM? = null

    override fun getContentViewLayoutId(): Int {
        return 0
    }

    override fun init(savedInstanceState: Bundle?) {
//        mLoadingController = LoadingController.create(this, refreshLayout, LoadingConfigDefault())
//                .setOnErrorRetryClickListener { doBusiness() }
    }

    override fun initPresenter() {
        mPagingViewModel = ViewModelProvider(this).get(TestPagingVM::class.java)
        mPagingViewModel?.pagingDataLiveData?.observe(this, Observer {
            onPagingDataSuccess(it)
        })
        mPagingViewModel?.pagingStateLiveData?.observe(this, Observer {
            onPagingStateChanged(it)
        })
    }

    override fun doBusiness() {
        //        pagingViewModel?.refresh()
    }

    private fun onPagingDataSuccess(data: PagingData<RoomListItemBean>?) {
//        mPagingAdapter.data = pagingData.list
//        if (pagingData.isFirstPage) {
//            rv_rooms.scrollToPosition(0)
//            mPagingAdapter.notifyDataSetChanged()
//        } else {
//            mPagingAdapter.notifyItemInserted(rv_rooms.headersCount + mPagingAdapter.itemCount)
//        }
    }

    private fun onPagingStateChanged(state: PagingState?) {
        state?.let {
            when (it) {
                PagingState.LOADING -> {
//                    refreshLayout.finishRefresh()
//                    mLoadingController?.showLoading()
                }
                PagingState.SUCCESS -> {
//                    mLoadingController?.dismissLoading()
//                    refreshLayout.finishRefresh()
//                    rv_rooms.loadMoreComplete()
                }
                PagingState.ERROR -> {
//                    mLoadingController?.showError(errorMessage)
//                    refreshLayout.finishRefresh()
                }
                PagingState.EMPTY -> {
//                    mLoadingController?.showEmpty()
                }
                PagingState.NO_MORE_DATA -> {
//                    rv_rooms.noMore(true)
                }
                PagingState.LOAD_MORE_FAILED -> {
//                    rv_rooms.loadMoreError()
                }
            }
        }
    }
}